
$(() => {
  // 搜索
//   有bog--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  let date = { page: 1, perpage: 6 };
  let skipAjax = () => {
    $(".search_btn").on("click", function () {
      // 获取输入狂内容
      let key = $(".search_txt").val().trim();
      console.log(key);
      //   传参
      date = {
        key,
        type: null,
        page:1,
        perpage:6,
      };
      //   渲染
      readAjax();
    });
  };
  skipAjax();
  //   bog--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  // 全部分类下拉列表
  let dropDownAjax = () => {
    myajax("get", "/index/category", {}, (res) => {
      let dropDownHtml = template("drop-down", res);
      $(".level_two").html(dropDownHtml);
    });
  };
  dropDownAjax();

  // 导航栏部分
  let navigationAjax = () => {
    myajax("get", "/index/category", {}, (res) => {
      let navigationHtml = template("navigation", res);
      $(".left_menu").html(navigationHtml);
    });
  };
  navigationAjax();

  //   有bog--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  // 文章阅读部分
  // 发送请求
  let readAjax = () => {
    myajax("get", "/index/search", date, (res) => {
      console.log(res);
      // 传数据给模板
      let readHtml = template("read", res.data);
      //  渲染
      $(".common_news_lists").html(readHtml);
      counttest1(res.data.totalCount);
    });
  };
  readAjax();
  //   有bog--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  // 文章热门排行
  let articleAjax = () => {
    myajax("get", "/index/rank", {}, (res) => {
      let articleHtml = template("article", res);
      $(".content_lists").html(articleHtml);
    });
  };
  articleAjax();

  //  <!-- 最新评论 -->
  let observerAjax = (res) => {
    myajax("get", "/index/latest_comment", {}, (res) => {
      let observerHtml = template("observer", res);

      $(".comment_list").html(observerHtml);
    });
  };
  observerAjax();

  // 焦点关注
  let focusAjax = () => {
    myajax("get", "/index/attention", {}, (res) => {
      let focusHtml = template("focus", res);
      $(".guanzhu_list").html(focusHtml);
    });
  };
  focusAjax();


// 有bog--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


  // 分页符
  let counttest1 = (count) => {
    layui.use("laypage", function () {
     let laypage = layui.laypage;

      //执行一个laypage实例
      laypage.render({
        elem: "test1", //注意，这里的 test1 是 ID，不用加 # 号
        count: count, //数据总数，从服务端得到
        limit: date.perpage, //显示条数
        curr: date.page, //页面开始位置
        //   ,groups:data.page //出现的页面数
        jump: function (obj, first) {
          //obj包含了当前分页的所有参数，比如：
          //   console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
          console.log(obj); //得到每页显示的条数

          //首次不执行
          if (!first) {
            //do something
            date.page = obj.curr;
            readAjax();
          }
        },
      });
    });
  };
  //   有bog--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
});

